Skip to content

Conversation

@colorbox
Copy link
Owner

Copy link

@garunitule garunitule left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

読みやすかったです。

vector<vector<int>> step_count(m, vector<int>(n, 0));
step_count[0][0] = 1;
for (int row = 0; row < m; ++row) {
for (int col = 0; col < n; ++col) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

略さずcolumnでも良いと思いました。

Comment on lines +13 to +15
if (y > 0) {
step += step_counter[x];
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

step += step_counter[x]というyを使わない計算にy > 0という条件が必要な理由がわかりませんでした。なくても良さそうにみえます。

Comment on lines +16 to +18
if (x > 0) {
step += step_counter[x - 1];
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xは1から始まるのでif (x > 0)は不要に見えます。

Comment on lines +10 to +11
for (int y = 0; y < m; ++y) {
for(int x = 1; x < n; ++x) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

インクリメントの変数にy, xを使っているのがわかりにくいと感じました。(しかも行を表す1つ目がy, 列を表す2つ目がxなのも読みにくさを増やしていそうです。)

2D matrixのインデックスにはrow, colやi, jを使うことをおすすめします。自分はrow, colが最もわかりやすいと感じます。

/*
単一の配列で実行するのを試す
*/
class Solution {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これでいいかなと思います:

class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<int> step_counter(n, 1);
        for (int row = 1; row < m; ++row) {
            for(int col = 1; col < n; ++col) {
                step_counter[col] += step_counter[col - 1];
            }
        }
        return step_counter.back();
    }
};

Comment on lines +8 to +10
if (col == 0 && row == 0) {
continue;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これはあえて書かなくても読めるだろうと感じました。
下のif2つはどのみちif-continueでもif-elseでもないため条件を同時に読む必要があるからです。(下2つのifを同時に読むとrow == 0かつcol == 0のとき何もしないことは自然と読み取れます。)

class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> grid(m, vector<int>(n, 0));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grid という変数名ですと、 grid の要素が何を表すのか分かりにくく感じました。自分なら num_paths と付けると思います。

public:
int uniquePaths(int m, int n) {
vector<vector<int>> grid(m, vector<int>(n, 0));
grid[0][0]=1;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= の両脇にスペースを空けることをお勧めいたします。

if (x > 0) {
grid[y][x] += grid[y][x - 1];
}
if(y > 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if のあとにスペースを空けることをお勧めいたします。

step_counter[0] = 1;

for (int y = 0; y < m; ++y) {
for(int x = 1; x < n; ++x) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for の後にスペースを空けることをお勧めいたします。

class Solution {
public:
int uniquePaths(int m, int n) {
vector<int> step_counter(n, 0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

格納されているものは経路の個数であり、ステップの個数ではないため、 step_counter と名付けるのは不自然に感じました。

}
}
}
return grid[m-1][n-1];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return grid.back().back(); の方が読み手にとってわかりやすいかなと思います。

public:
int uniquePaths(int m, int n) {
vector<vector<int>> grid(m, vector<int>(n, 0));
grid[0][0] = 1;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人の趣味ですが、grid.front().front() = 1; の方がより意図が明確になってよいかなと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants